package sugar

import (
	"fmt"
	"strconv"
	"time"
)

func FormatTimeToDateOnly(t time.Time) string {
	return t.Format(time.DateOnly)
}

func FormatTimeToDateTime(t time.Time) string {
	return t.Format(time.DateTime)
}

func StringToTime(input string) (*time.Time, error) {
	if input == "" {
		return nil, nil
	}

	t, err := time.ParseInLocation(time.DateTime, input, time.Local)
	if err != nil {
		return nil, fmt.Errorf("input parameter '%s' is invalid: %v", input, err)
	}

	return &t, nil
}

func StringToUnixTimeWithErr(input string) (int64, error) {

	t, err := StringToTime(input)
	if err != nil {
		return 0, fmt.Errorf("input parameter '%s' is invalid: %v", input, err)
	}

	return t.Unix(), nil
}

func StringToUnixTime(input string) string {

	t, err := StringToUnixTimeWithErr(input)
	if err != nil {
		return ""
	}

	return strconv.FormatInt(t, 10)
}
